package com.yinhe.music.yhmusic.album.list;

import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.IBaseModel;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.model.AlbumInfo;
import com.yinhe.music.yhmusic.network.response.ResponseTransformer;

import static com.yinhe.music.yhmusic.constants.IConstants.SINGER_DETAIL_ALBUM;

/**
 * @author 何晓华
 */
public class AlbumListPresenter extends BasePresenter<IBaseModel, IAlbumListContract.IAlbumListView> implements IAlbumListContract.IAlbumListPresenter {
    @Override
    public void getAlbumList(int page, int pageSize) {

        if (getView() == null) {
            return;
        }

        getView().showLoading();

        addSubscription(mModel.getAlbumList(page, pageSize)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(albumInfo -> {
                            getView().hideLoading();
                            getView().setAlbumListUI(albumInfo);
                        },
                        throwable -> {
                            getView().hideLoading();
                            getView().showMessage(throwable);
                        }));
    }

    @Override
    public void getSearchAlbumList(int page, int pageSize, String keyword) {
        if (getView() == null) {
            return;
        }

        if (page == 1) {
            getView().showLoading();
        }

        addSubscription(mModel.getSearchList(page, pageSize, keyword, IConstants.ALBUM)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(searchInfo -> {
                            getView().hideLoading();
                            getView().setAlbumListUI(new AlbumInfo(searchInfo.getPageNum(), searchInfo.getAlbumList()));
                        },
                        throwable -> {
                            getView().hideLoading();
                            getView().showMessage(throwable);
                        }));
    }

    @Override
    public void getSingerAlbumList(int from, int page, int pageSize, int id) {
        if (getView() == null) {
            return;
        }

        getView().showLoading();

        String singerType;
        if (from == SINGER_DETAIL_ALBUM) {
            singerType = IConstants.SINGER;
        } else {
            singerType = IConstants.INSTRUMENT;
        }

        addSubscription(mModel.getSingerAlbum(singerType, page, pageSize, id)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(albumInfo -> {
                            getView().hideLoading();
                            getView().setAlbumListUI(albumInfo);
                        },
                        throwable -> {
                            getView().hideLoading();
                            getView().showMessage(throwable);
                        }));
    }

    @Override
    public void getCollectAlbumList(int page, int pageSize) {
        if (getView() == null) {
            return;
        }

        getView().showLoading();

        addSubscription(mModel.getCollectAlbum(page, pageSize)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(albumInfo -> {
                            getView().hideLoading();
                            getView().setAlbumListUI(albumInfo);
                        },
                        throwable -> {
                            getView().hideLoading();
                            getView().showMessage(throwable);
                        }));
    }
}
